package com.wufun.overseas.view;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wufun.overseas.R;
import com.wufun.overseas.models.bean.GameTransferBean;
import com.wufun.overseas.models.bean.GameTransferStuatusBean;
import com.wufun.overseas.utils.MyImageLoader;

import org.greenrobot.eventbus.EventBus;

/**
 * Author: hest
 * Date:  2016/5/4
 * 面传中选游戏列表
 */
public class GameTransferListItem extends RelativeLayout {
    private static final String TAG = GameTransferListItem.class.getSimpleName();
    TextView    game_name_txt;
    TextView    game_size_txt;
    TextView    transfer_txt;
    CheckBox    chb;
    FrameLayout checkLayout;
    SimpleDraweeView icon_img;
    GameTransferBean mBean;
    int                           mPosition;
    boolean                       mCheck;
    SparseArray<GameTransferBean> mArray;
    Context                       mContext;

    public GameTransferListItem(Context context) {
        super(context);
        this.mContext = context;
        initView(context);
    }

    private void initView(Context context){
        View.inflate(context, R.layout.game_transfer_list_item, GameTransferListItem.this);
        game_name_txt = (TextView) this.findViewById(R.id.game_transfer_item_game_name_txt);
        game_size_txt = (TextView) this.findViewById(R.id.game_transfer_item_game_size_txt);
        transfer_txt = (TextView) this.findViewById(R.id.game_transfer_list_item_transfer_status_txt);
        chb = (CheckBox) this.findViewById(R.id.game_transfer_list_item_transfer_status_chk);
        icon_img = (SimpleDraweeView) this.findViewById(R.id.game_transfer_item_game_icon_img);
        checkLayout = (FrameLayout) this.findViewById(R.id.checkLayout);
    }

    public void bind(int position, SparseArray<GameTransferBean> array, boolean sending) {
        if (array == null) {
            return;
        }
        mPosition = position;
        mArray = array;
        mBean = array.valueAt(mPosition);
        game_name_txt.setText(mBean.getGameName());
        game_size_txt.setText(mBean.getSize() + "M");
        mCheck = mBean.isSelectedTransfer();
        MyImageLoader.load(  icon_img,R.drawable.main_normal_icon,mBean.getIconUrl());
        Log.d(TAG, "bind: 图片地址" + mBean.getIconUrl());
//        chb.setOnCheckedChangeListener(null);
//        chb.setEnabled(!sending);
//        chb.setChecked(mCheck);
//        chb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                Log.d(TAG, "changeCheckboxStatus: " + isChecked + "位置" + mPosition);
//                mBean.setSelectedTransfer(isChecked);
//                mArray.setValueAt(mPosition, mBean);
//                EventBus.getDefault().post(mBean);
//            }
//        });

        chb.setEnabled(false);
        chb.setChecked(mCheck);
        checkLayout.setOnClickListener(null);
        checkLayout.setEnabled(!sending);

        setOnClickListener(null);
        OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = !chb.isChecked();

                chb.setChecked(isChecked);

                Log.d(TAG, "changeCheckboxStatus: " + isChecked + "位置" + mPosition);
                mBean.setSelectedTransfer(isChecked);
                mArray.setValueAt(mPosition, mBean);
                EventBus.getDefault().post(mBean);
            }
        };
//        checkLayout.setOnClickListener(listener);
        checkLayout.setClickable(false);
        if (mBean.getTransferStatus()== GameTransferStuatusBean.UN_KNOW)
        setOnClickListener(listener);

        setTransferStatus();
    }


    //设置传输状态
    void setTransferStatus() {
        Log.d(TAG, "setTransferStatus: ");
        int status = mBean.getTransferStatus();
        if (status == GameTransferStuatusBean.UN_KNOW) {
//            chb.setVisibility(VISIBLE);
            checkLayout.setVisibility(VISIBLE);
        } else {
//            chb.setVisibility(GONE);
            checkLayout.setVisibility(GONE);

            transfer_txt.setVisibility(VISIBLE);
        }
        transfer_txt.setText("");
        switch (status) {
            case GameTransferStuatusBean.WAITING:
                transfer_txt.setText(R.string.waiting_status_game_transfer);
                break;
            case GameTransferStuatusBean.TRANSFERING:
                transfer_txt.setText(mBean.getProgerss() + "%");
                break;
            case GameTransferStuatusBean.TRANSFER_SUCCESS:
                transfer_txt.setText(R.string.transfer_success_status_game_transfer);
                transfer_txt.setTextColor(mContext.getResources().getColor(R.color.blue_game_transfer));
                break;
            case GameTransferStuatusBean.TRANSFER_FAILED:
                transfer_txt.setTextColor(Color.RED);
                transfer_txt.setText(R.string.transfer_failed_status_game_transfer);
                break;
            case GameTransferStuatusBean.ZIP:
                transfer_txt.setText(R.string.transfer_zip_status_game_transfer);
                break;
            default:
                break;
        }
    }
}
